package com.google.ytd;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.jason.unpluggedsuperstar.R;

public class YTD {
	
	public static final String NEW_ASSIGNMENT_UPDATE = "com.google.ytd.NEW_ASSIGNMENT_UPDATE";
	public static final String ALARM_ACTION = "com.google.ytd.ALARM_ACTION";
	public static final String SHARED_PREF_NAME = "com.google.ytd.PREF";
	public static final String YTD_DOMAIN = "ytd_domain";
	
	private Context mContext = null;
	private SharedPreferences preferences = null;
	private String ytdDomain = null;
	private accessListener mAL;
	
	public YTD(Context context){
		
		this.mContext = context;
	    
	    this.preferences = context.getSharedPreferences(SHARED_PREF_NAME, Activity.MODE_PRIVATE);
	    this.ytdDomain = this.getSharedPreference(YTD_DOMAIN);
	    
	    if(this.ytdDomain == null){
	    	this.ytdDomain = getYTDDomain();
	    	setSharedPreference(YTD_DOMAIN,this.ytdDomain);
	    }
	}
	
	/****
	 * interface
	 */
	public void setOnAccessListener(accessListener al){
		mAL = al;
	}
	
	public interface accessListener{
		public void onVerified();
		public void onError(String error);
	}
	
	/****
	 * private
	 */
	private String getSharedPreference(String key) {
	    return preferences.getString(key, null);
	}
	
	private void setSharedPreference(String key, String value) {
	    SharedPreferences.Editor editor = this.preferences.edit();
	    editor.putString(key, value);
	    editor.commit();
	  }
	
	/****
	 * public
	 */
	public void checkCriteria(accessListener al){
		mAL = al;
		
		if (this.ytdDomain == null) {
	    	mAL.onError("domain not set");
	    }else {
	    	mAL.onVerified();
	    }
	}
	
	public String getYTDDomain(){
		Resources res = mContext.getResources();
		return res.getString(R.string.default_ytd_domain);
	}
	
	public String getUserAccount(){
		AccountManager accountManager = AccountManager.get(mContext);
		Account[] accts = accountManager.getAccountsByType("com.google");
		
		if (accts.length == 0) {
			return "";
	    }
		
		String firstEmail = "";
		for (Account acct : accts) {
			if(acct.name.length() > 0){
				firstEmail = acct.name;
				//break;
			}
		}
		
		return firstEmail;
	}
	
	public static boolean haveNetworkConnection(Context context) {
	    boolean haveConnectedWifi = false;
	    boolean haveConnectedMobile = false;

	    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
	    for (NetworkInfo ni : netInfo) {
	        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
	            if (ni.isConnected())
	                haveConnectedWifi = true;
	        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
	            if (ni.isConnected())
	                haveConnectedMobile = true;
	    }
	    return haveConnectedWifi || haveConnectedMobile;
	}
	
	public static boolean haveWifiConnection(Context context){
		boolean haveConnectedWifi = false;
		
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
	    for (NetworkInfo ni : netInfo) {
	        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
	            if (ni.isConnected())
	                haveConnectedWifi = true;
	    }
	    
	    return haveConnectedWifi;
	}
}
